package com.neusoft.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.domain.*;
import com.neusoft.domain.Vo.OrderDetailetVo;
import com.neusoft.domain.Vo.OrderVo;
import com.neusoft.mapper.FoodMapper;
import com.neusoft.mapper.OrderDetailetMapper;
import com.neusoft.mapper.OrderMapper;
import com.neusoft.mapper.ShopMapper;
import com.neusoft.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderDetailetMapper orderDetailetMapper;
    @Autowired
    private FoodMapper foodMapper;
    @Override
    public PageResult<Order> pageSelectOrder(Integer pageNum, Integer pageSize, String userId, Integer businessId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Order>list=orderMapper.pageSelectOrder(userId,businessId);
        for (Order order : list) {
            switch (order.getOrderStatus())
                {
                case 0:
                    order.setOrderStatusName("未支付");
                    break;
                case 1:
                    order.setOrderStatusName("已支付");
                    break;
                case 2:
                    order.setOrderStatusName("已取消");
                    break;
                case 3:
                    order.setOrderStatusName("待发货");
                    break;
                case 4:
                    order.setOrderStatusName("已派送");
                    break;
                case 5:
                    order.setOrderStatusName("收到");
                    break;
                case 6:
                    order.setOrderStatusName("待评价");
                    break;
                case 7:
                    order.setOrderStatusName("已评价");
                    break;
                default:
                        break;
                }

        }
        PageInfo<Order>PageResult=PageInfo.of(list);
        return new PageResult<>(PageResult.getTotal(),PageResult.getList());
    }

    @Override
    public void changeOrderStatus(Integer orderId, Integer orderStatus) {
        orderMapper.changeOrderStatus(orderId,orderStatus);
    }

    @Override
    public OrderVo getOrderDetailByOrderId(Integer orderId) {
        // 查询该订单的基本信息
        Order order = orderMapper.getOrdersById(orderId);
        System.out.println(order);
        OrderVo orderVo =new OrderVo();
        if (order!= null) {
            BeanUtils.copyProperties(order,orderVo);
            // 封装商家信息
            Shop shop = shopMapper.getBusinessById(order.getBusinessId());

            orderVo.setShop(shop);

            // 封装订单对应的菜品信息
            List<OrderDetailet> orderDetailets = orderDetailetMapper.listOrderDetailetByOrderId(orderId);
            List<OrderDetailetVo> orderDetailetVos = new ArrayList<>();
            for (OrderDetailet orderDetailet : orderDetailets) {
                OrderDetailetVo orderDetailetVo = new OrderDetailetVo();
                BeanUtils.copyProperties(orderDetailet,orderDetailetVo);
                orderDetailetVos.add(orderDetailetVo);
            }
            for (OrderDetailetVo orderDetailet : orderDetailetVos) {
                Integer foodId = orderDetailet.getFoodId();
                Food food = foodMapper.getFoodById(foodId);
                orderDetailet.setFood(food);
            }

            // 再把集合添加到订单对象中
            orderVo.setList(orderDetailetVos);

        }

        return orderVo;
    }

    @Override
    public Data getData(Integer id) {
        List<Order> ordersByBusinessId = orderMapper.getOrdersByBusinessId(id);
        Integer orderToTal=0;
        Double totalPrice=0.0;
        Integer commentToTal=0 ;
        Integer personNumber=0;
        for (Order order : ordersByBusinessId) {
            orderToTal++;
            totalPrice+=order.getOrderTotal();
            if (order.getComment()!=null){
                commentToTal++;
            }
            if (order.getUserId()!=null){
                personNumber++;
            }
        }
        return new Data(orderToTal,totalPrice,commentToTal,personNumber);
    }
}
